// pages/reportDetail/reportDetail.js
import * as FetchAPI from '../../wxapi/api/dispatch.js'
import * as Utils from '../../utils/util.js'
const tableListHeadOne = [
  { key: 'brandName', title: '客户', width: '160' },
  { key: 'goodsNameNum', title: '站点', width: '120' },
  { key: 'siteNum', title: '商品数', width: '120' },
  { key: 'totalVolume', title: '体积', width: '120' },
  { key: 'totalWeight', title: '重量', width: '120' }
]
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loadMoreHidden: true,
    scrollHeight: 0,
    scrollTop: 0,
    waybillStatus: 1, // 查询类型 1,尾单  2，已规划
    topTitle: '',
    tableListHead: [],
    tableList: [],
    loadingData: false,
    pageNum: 1, //当前页数
    pageSize: 3, //一页数量
    total: 1, // 总条数
    searchObj: {
      city: '',
      brand: '',
    },
    repeartFalg: true, // 重复请求
  },
  // 跳转主页
  toHome: function () {
    wx.navigateTo({
      url: '../reportList/reportList'
    })
  },
  // 请求
  getList(that, page, size, flag) {
    // 下拉时清空查询条件
    if (flag == 'down') {
      //
    } else if (flag == 'lower') {
      wx.showNavigationBarLoading()
    }
    const userInfo = wx.getStorageSync('userInfo')
    // 查询条件
    const toParams = {
      waybillStatus: that.data.waybillStatus,    
      pageSize: size,
      pageNum: page,
      companyId: userInfo.manager.companyId
    }
    // loading
    wx.showLoading({
      title: '加载中...',
      icon: 'loading',
      mask: true
    })
    const nowList = that.data.tableList
    // 发请求
    FetchAPI.getDispatchInfoDetails(toParams).then(res => {
      console.log('getDispatchInfoDetails()-res', res)
      const resData = res.data
      if (res.code === 0 && resData) {
        this.setData({
          pageNum: resData.pageNum,
          pageSize: resData.pageSize,
          tableList: nowList.concat(resData.content),
          total:resData.totalRows
        })
        // this.isHaveMore(resData.totalRows)
      }
      wx.hideLoading()
      that.setData({
        loadMoreHidden: false, // 显示更多
      })
    }).catch(errTwo => {
      wx.hideLoading()
      that.setData({
        loadMoreHidden: false, // 显示更多
      })
      console.log('error', errTwo)
    }) 
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    let newoptions = JSON.parse(options.caroption)
    console.log('reportDetail-options', options, 'newoptions', newoptions)
    // 设置title
    if (newoptions.type === 'plannedNum') {
      this.setData({
        topTitle: Utils.parseTime(new Date(), '{y}-{m}-{d}') + '已规划信息',
        waybillStatus: '2',
      })
    } else {
      this.setData({
        topTitle: Utils.parseTime(new Date(), '{y}-{m}-{d}') + '尾单信息',
        waybillStatus: '1',
      })
    }
    that.setData({
      tableListHead: tableListHeadOne
    })
    // 获取高度 
    wx.getSystemInfo({ 
      success: function (res) { 
        console.log(res.windowHeight); 
        that.setData({ 
          scrollHeight: res.windowHeight 
        }); 
      } 
    }); 
    // 初次请求
    that.getList(that, 1, 3, 'down')
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('onPullDownRefresh()下拉刷新')
    // wx.showNavigationBarLoading() //在标题栏中显示加载
    this.setData({
      tableList: [],
      scrollTop: 0
    })
    const that = this
    const page = 1
    const size = 3
    this.getList(that, page, size, 'down')
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('onReachBottom()上拉加载')
    this.isHaveMore(this.data.total)
  },
  // 是否还有下一页，如果没有就禁止上拉刷新
  isHaveMore: function (total) {
    const that = this
    const now_show_num = that.data.tableList.length
    const get_total = total
    console.log('tableList', now_show_num, 'total', total)
    if (now_show_num < get_total) {
      // 展示的条数 小于 后端总条数 还可以下拉加载更多
      that.setData({
        loadMoreHidden: true, // 显示更多
      })
      const thatData = that.data
      const page = thatData.pageNum; //当前页数
      const size = thatData.pageSize; //数量
      const pageAdd = parseInt(page) + 1; //下一页
      // 请求
      that.getList(that, pageAdd, size, 'lower');
    } else if (now_show_num >= get_total) {
      // 展示的条数 等于 后端总条数 没有更多数据 不能进行下拉加载
      wx.showToast({
        title: '没有更多数据了',
        duration: 2000,
        icon: 'none',
        mask: true
      })
    }
  },
  //
  scrolltoupper() {
    console.log("滚动到顶部-向下拉刷新");
    // const that = this
    // this.setData({
    //   tableList: [],
    //   scrollTop: 0
    // })
    // const page = 1
    // const size = 3
    // this.getList(that, page, size, 'down')
  },
  //
  scrolltolower() {
    console.log("滚动到底部-向上拉加载");
    // this.isHaveMore(this.data.total)
  },
  bindscroll(event){
    console.log("滚动中~");
    const that = this
    this.setData({ 
      scrollTop: event.detail.scrollTop 
     }); 
  },
  scrollToTop() {
    this.setAction({
      scrollTop: 0
    })
  },
    /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    // console.log('onShow()-')
    // if(this.data.refresh){
    //   this.setData({
    //     refresh: false
    //   })
    //   this.onLoad();
    // }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
